﻿#ifndef SQLITEUTIL_H
#define SQLITEUTIL_H
#include <QDebug>
#include <QtSql/qsql.h>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QList>
#include <QObject>
#include <QTime>
#include <QFile>
#include <QDataStream>
#include <QFileInfo>
#include <QVector>
#include <QtMath>
#include <Common/common.h>

class SqliteUtil
{
public:
    SqliteUtil();
    ~SqliteUtil();

    void init(QString db);

    int excute_sql_file(QString sql_file_path);

    bool createConnection(QString dbname);

    bool disConnection();

    bool createTabel(QString fileName);

    bool isExistTab(QString tbname);


    void strToSql(QString& str);

    void deleteSingleData(int id, QString tbname);

    void deleteDefectschemes(int id);


    // 插入DefectScheme并返回生成的schemeId
    int insertDefectScheme(const DefectScheme &scheme);

    QList<DefectScheme> queryAllDefectSchemes();

    bool queryDefectScheme(int scheme_id, DefectScheme &record);

    bool queryDefectSettings(int scheme_id, QList<DefectSetting> &records);

    bool queryGeneralParams(int scheme_id, QList<GeneralParam> &records);

    bool queryQualityGrades(int scheme_id, QList<QualityGrade> &records);

    bool queryEdgeParams(int scheme_id, QList<EdgeParam> &records);

    bool queryFixedLengths(int scheme_id, QList<FixedLength> &records);

    QList<WoodProcessHistoryData> getWoodProcessHistoryData();

    bool updateDefectScheme(int scheme_id, const DefectScheme& Scheme);
    bool updateDefectSettings(int scheme_id, const QList<DefectSetting>& settings);
    bool updateGeneralParams(int scheme_id, const QList<GeneralParam>& params);
    bool updateQualityGrades(int scheme_id, const QList<QualityGrade>& grades);
    bool updateEdgeParams(int scheme_id, const QList<EdgeParam>& params);
    bool updateFixedLengths(int scheme_id, const QList<FixedLength>& lengths);


    bool insertPlanLine(const T_PlanLine &planLine);
    QList<T_PlanLine> queryAllPlanLines();

    /* Batch*/
    QList<T_Batch> queryAllBatchs();
    QList<T_BatchLine> queryBatchLines(const int& batch_id);
    T_Batch queryBatchs(int batch_id);
    bool insertBatch(const T_Batch &batch);
    bool insertBatchLines(const QList<T_BatchLine> &batchLines);

    /* PlanLine*/
    T_PlanLine queryPlanLine(int plan_id);
    bool updatePlanLine(const T_PlanLine& line);

private:
    QFile file;
    int m_nRow;
    int m_nRowMax;


    QString m_strDbPath;
    QString m_strSqlPath;
    QString m_DB;

    bool m_bDiscontinue;
    QString m_sTbname;
};

#endif // SQLITEUTIL_H
